package com.example.farmeri.common;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by weiqishan on 2020/5/28.
 * 解决有着横向滑动冲突的RecycleView
 * 如果纵向滑动，滑动事件消耗掉
 * 如果横向滑动，滑动事件分发给下级
 */
public class HRecycleView extends RecyclerView {

    /**
     * 第一次滑动的方向
     * -1：没有定义方向
     * 0：水平方向
     * 1：垂直方向
     */
    private Integer orientation = -1;

    public HRecycleView(@NonNull Context context) {
        super(context);
    }

    public HRecycleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HRecycleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptHoverEvent(MotionEvent event) {
        //是否消耗事件
        Boolean intercept = false;
        //获取滑动的值
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                intercept = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (orientation == -1){
                    intercept = y>0 ? true:false;
                }
                return false;
            default:
                break;
        }
        return intercept;
    }
}
